package com.cadre.common.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/* loaded from: ZipUtil.class */
public class ZipUtil {
    private String zipFileName;
    private String sourceFileName;

    public ZipUtil(String zipFileName, String sourceFileName) {
        this.zipFileName = zipFileName;
        this.sourceFileName = sourceFileName;
    }

    public void zip() throws IOException {
        System.out.println("压缩中......");
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(this.zipFileName));
        BufferedOutputStream bos = new BufferedOutputStream(out);
        File sourceFile = new File(this.sourceFileName);
        compress(out, bos, sourceFile, sourceFile.getName());
        bos.close();
        out.close();
        System.out.println("压缩完成");
    }

    private void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws IOException {
        if (!sourceFile.exists()) {
            return;
        }
        if (sourceFile.isFile()) {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream fins = new FileInputStream(sourceFile);
            BufferedInputStream bins = new BufferedInputStream(fins, 1024);
            byte[] buffer = new byte[1024];
            while (true) {
                int nNumber = bins.read(buffer);
                if (nNumber != -1) {
                    out.write(buffer, 0, nNumber);
                } else {
                    bins.close();
                    fins.close();
                    return;
                }
            }
        } else {
            File[] files = sourceFile.listFiles();
            if (files.length == 0) {
                System.out.println("base path：" + base);
                out.putNextEntry(new ZipEntry(base + "/"));
                return;
            }
            for (File file1 : files) {
                compress(out, bos, file1, base + "/" + file1.getName());
            }
        }
    }

    public static void unzip(String filePath) {
        BufferedOutputStream bos = null;
        ZipInputStream zis = null;
        File source = new File(filePath);
        if (source.exists()) {
            try {
                zis = null;
                bos = null;
                try {
                    zis = new ZipInputStream(new FileInputStream(source));
                    while (true) {
                        ZipEntry entry = zis.getNextEntry();
                        if (entry == null || entry.isDirectory()) {
                            break;
                        }
                        File target = new File(source.getParent(), entry.getName());
                        if (!target.getParentFile().exists()) {
                            target.getParentFile().mkdirs();
                        }
                        bos = new BufferedOutputStream(new FileOutputStream(target));
                        byte[] buffer = new byte[10240];
                        while (true) {
                            int read = zis.read(buffer, 0, buffer.length);
                            if (read != -1) {
                                bos.write(buffer, 0, read);
                            }
                        }
                        // TODO 解开注释
//                        bos.flush();
                    }
                    zis.closeEntry();
                    IOUtil.closeQuietly(zis, bos);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } catch (Throwable th) {
                IOUtil.closeQuietly(zis, bos);
                throw th;
            }
        }
    }

    public static void unzip(File source, String filePath) {
        BufferedOutputStream bos;
        ZipInputStream zis;
        try {
            zis = null;
            bos = null;
            try {
                zis = new ZipInputStream(new FileInputStream(source));
                while (true) {
                    ZipEntry entry = zis.getNextEntry();
                    if (entry == null || entry.isDirectory()) {
                        break;
                    }
                    File target = new File(filePath, entry.getName());
                    System.out.println(target.getParentFile());
                    if (!target.getParentFile().exists()) {
                        target.getParentFile().mkdirs();
                    }
                    bos = new BufferedOutputStream(new FileOutputStream(target));
                    byte[] buffer = new byte[10240];
                    while (true) {
                        int read = zis.read(buffer, 0, buffer.length);
                        if (read != -1) {
                            bos.write(buffer, 0, read);
                        }
                    }
                    // TODO 解开注释
//                    bos.flush();
                }
                zis.closeEntry();
                IOUtil.closeQuietly(zis, bos);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (Throwable th) {
//            IOUtil.closeQuietly(zis, bos);
            throw th;
        }
    }

    public static void zipUncompress(String inputFile, String destDirPath) throws Exception {
        ZipFile zipFile = null;
        File srcFile = new File(inputFile);
        if (!srcFile.exists()) {
            throw new Exception(srcFile.getPath() + "所指文件不存在");
        }
        try {
            System.out.println("===>ZIP_ENCODE:GBK");
            zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
            try {
                Enumeration<?> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) entries.nextElement();
                    if (entry.isDirectory()) {
                        String str = destDirPath + "/" + entry.getName();
                        srcFile.mkdirs();
                    } else {
                        File targetFile = new File(destDirPath + "/" + entry.getName());
                        if (!targetFile.getParentFile().exists()) {
                            targetFile.getParentFile().mkdirs();
                        }
                        targetFile.createNewFile();
                        InputStream is = zipFile.getInputStream(entry);
                        FileOutputStream fos = new FileOutputStream(targetFile);
                        byte[] buf = new byte[1024];
                        while (true) {
                            int len = is.read(buf);
                            if (len == -1) {
                                break;
                            }
                            fos.write(buf, 0, len);
                        }
                        fos.close();
                        is.close();
                    }
                }
                if (null != zipFile) {
                    zipFile.close();
                }
                System.gc();
            } catch (Exception ex) {
                ex.printStackTrace();
                if (null != zipFile) {
                    zipFile.close();
                }
                System.gc();
            }
        } catch (Throwable th) {
            if (null != zipFile) {
                zipFile.close();
            }
            System.gc();
            throw th;
        }
    }
}
